import { ref } from 'vue';

import {
  queryStackChart,
  queryStackChartel,
  getQueryLowerAy,
  deaqueryVaryTrend,
  deaqueryVaryTrendel,
  deaqueryXqdwPieChart,
  deaqueryXqdwPieChartel,
  queryXqdwDealTop3,
  queryXqdwCallTop3,
} from '@/api/intelligentanalysis/alert';

// 警情分类统计
export const querybarCharts = (queryForm, code, apiType) => {
  const barData = ref<any>({
    xData: [],
    yData: [],
    newData: [],
  });
  barData.value.yData = [];
  barData.value.xData = [];
  barData.value.newDara = [];
  const obj = ref<any>({
    name: '总数',
    type: 'bar',
    barGap: '-100%',
    barWidth: 30,
    label: {
      position: 'top',
      normal: {
        show: true, //显示数值
        position: 'top', // 在上方显示
        distance: 10, // 距离图形元素的距离。当 position 为字符描述值（如 'top'、'insideRight'）时候有效。
        verticalAlign: 'middle',
        // formatter: '{c}',
        textStyle: { color: '#000', fontSize: 12, textAline: 'center' }, //设置数值颜色
      },
    },
    itemStyle: {
      normal: {
        color: 'rgba(128, 128, 128, 0)', // 设置背景颜色为透明
      },
    },
    data: [],
  });
  obj.value.data = [];
  queryForm.dealSummaryCodes = apiType == 'fq' ? code : '';
  queryForm.callSummaryCodes = apiType == 'jq' ? code : '';
  if (apiType == 'fq') {
    queryStackChart(queryForm).then(res => {
      if (res.data.xList) {
        res.data.xList.forEach(item => {
          obj.value.data.push(item.caseNum);
          item.data = [];
          item.childCode = [];
          barData.value.xData.push(item.name);
          res.data.yList.forEach(el => {
            if (el.code == item.code) {
              el.name = el.childName;
              el.parentName = item.parentName;
              el.percent = item.percent;
              el.caseNum = item.caseNum;
              item.childCode.push(el.childCode);
              el.code = item.code;
              barData.value.yData.push(el);
            }
          });
        });
        barData.value.newDara = res.data.xList;
        barData.value.yData = barData.value.yData.map(item => ({
          stack: 'value',
          type: 'bar',
          name: item.name,
          data: item.valueList,
          parentName: item.parentName,
          percent: item.percent,
          caseNum: item.caseNum,
          code: item.code,
          childCode: item.childCode,
          barWidth: '30',
        }));
        barData.value.yData.push(obj.value);
        return barData.value;
      }
    });
  } else {
    queryStackChartel(queryForm).then(res => {
      if (res.data.xList) {
        res.data.xList.forEach(item => {
          obj.value.data.push(item.caseNum);
          item.data = [];
          item.childCode = [];
          barData.value.xData.push(item.name);
          res.data.yList.forEach(el => {
            if (el.code == item.code) {
              el.name = el.childName;
              el.parentName = item.parentName;
              el.percent = item.percent;
              el.caseNum = item.caseNum;
              item.childCode.push(el.childCode);
              el.code = item.code;
              barData.value.yData.push(el);
            }
          });
        });
        barData.value.newDara = res.data.xList;
        barData.value.yData = barData.value.yData.map(item => ({
          stack: 'value',
          type: 'bar',
          name: item.name,
          data: item.valueList,
          parentName: item.parentName,
          percent: item.percent,
          caseNum: item.caseNum,
          topList: item.topList,
          code: item.code,
          childCode: item.childCode,
          barWidth: '30',
        }));
        barData.value.yData.push(obj.value);
      }
    });
    return barData.value;
  }
};

//下级同环比数据
export const queryLowerAy = (queryForm, code, level, apiType) => {
  return new Promise(resolve => {
    const ayData = ref<any>();
    queryForm.dealSummaryCodes = apiType == 'fq' ? code : '';
    queryForm.callSummaryCodes = apiType == 'jq' ? code : '';
    queryForm.level = level;
    getQueryLowerAy(queryForm).then(res => {
      if (res.data) {
        ayData.value = res.data;
      }
      resolve(ayData.value);
    });
  });
};

//趋势图
export const handlequeryVary = (queryForm, code, apiType) => {
  return new Promise(resolve => {
    const trendList = ref<any>();
    queryForm.dealSummaryCodes = apiType == 'fq' ? code : '';
    queryForm.callSummaryCodes = apiType == 'jq' ? code : '';
    if (apiType == 'fq') {
      deaqueryVaryTrend(queryForm).then(res => {
        trendList.value = res.data;
        resolve(trendList.value);
      });
    } else {
      deaqueryVaryTrendel(queryForm).then(res => {
        trendList.value = res.data;
        resolve(trendList.value);
      });
    }
  });
};

//辖区警情统计
export const xqPieChart = (queryForm, code, apiType) => {
  return new Promise(resolve => {
    const pieData = ref<any>();
    queryForm.dealSummaryCodes = apiType == 'fq' ? code : '';
    queryForm.callSummaryCodes = apiType == 'jq' ? code : '';
    if (apiType == 'fq') {
      deaqueryXqdwPieChart(queryForm).then(res => {
        res.data.forEach(item => {
          item.value = item.caseNum;
        });
        pieData.value = res.data;
        resolve(pieData.value);
      });
    } else {
      deaqueryXqdwPieChartel(queryForm).then(res => {
        res.data.forEach(item => {
          item.value = item.caseNum;
        });
        pieData.value = res.data;
        resolve(pieData.value);
      });
    }
  });
};

// top3
export const xqdwClassify = (queryForm, code, apiType) => {
  return new Promise(resolve => {
    const topData = ref<Array<any>>([]);
    queryForm.dealSummaryCodes = apiType == 'fq' ? code : '';
    queryForm.callSummaryCodes = apiType == 'jq' ? code : '';
    if (apiType == 'fq') {
      queryXqdwDealTop3(queryForm).then(res => {
        topData.value = res.data.slice(0, 3);
        resolve(topData.value);
      });
    } else {
      queryXqdwCallTop3(queryForm).then(res => {
        topData.value = res.data.slice(0, 3);
        resolve(topData.value);
      });
    }
  });
};

export const navForEach = value => {
  const obj: any = [];
  value.forEach(el => {
    obj.push({
      name: el.vname,
      code: el.vcode,
      caseNum: el.caseNum,
      children: el.childTreeList,
      level: el.level,
      chainCaseNum: el.chainCaseNum, //环比数量
      yoyCaseNum: el.yoyCaseNum, //同比数量
      contentValue: thJudge(el.percent, el.yoyPercent),
    });
  });
  return obj;
};

export const navForEachBar = value => {
  const obj: any = [];
  value.shift();
  value.forEach(el => {
    obj.push({
      name: el.name,
      code: el.code,
      caseNum: el.caseNum,
      level: el.level,
      contentValue: thJudge(el.hbPercent, el.tbPercent),
    });
  });
  return obj;
};

//同环比判断
export const thJudge = (chainPercent, yoyPercent) => {
  const value =
    '环比' +
    (parseFloat(chainPercent) > 0
      ? '上升' + chainPercent + '%'
      : chainPercent == '-'
      ? chainPercent + '%'
      : '下降' + chainPercent.split('-')[1] + '%') +
    '，同比' +
    (parseFloat(yoyPercent) > 0
      ? '上升' + yoyPercent + '%'
      : yoyPercent == '-'
      ? yoyPercent + '%'
      : '下降' + yoyPercent.split('-')[1] + '%');
  return value;
};

//数量判断
export const quantity = value => {
  const contentValue = ref<any>([]);
  value.shift();
  const valueShift = value;
  valueShift.forEach(el => {
    contentValue.value.push(
      el.name + el.caseNum + '起，' + thJudge(el.hbPercent, el.tbPercent) + '；',
    );
  });
  return contentValue.value;
};

//数量判断
export const xqTop = value => {
  const xqValue = ref<any>([]);
  if (value) {
    value.topList.forEach(el => {
      xqValue.value.push({
        caseNum: el.caseNum ? el.caseNum : '-',
      });
      xqValue.value.push({
        caseNum: el.percent ? el.percent + '%' : '-',
      });
    });
    return xqValue.value;
  }
};

export const xqtitle = value => {
  const xqValue = ref<any>([]);
  if (value) {
    value.forEach(el => {
      xqValue.value.push({
        name: el.name,
      });
      xqValue.value.push({
        name: '占比',
      });
    });
    return xqValue.value;
  }
};
